Skip to content

Conversation

@V3RON
Copy link
Contributor

@V3RON V3RON commented Jan 7, 2026

Description

The module mocking system has been rewritten to improve compatibility with different versions of React Native. Instead of fully overwriting Metro's module system, the new implementation surgically redirects responsibility for imports to Harness, allowing for better integration with various React Native versions while maintaining the same mocking capabilities. The module mocking API has been slightly modified as part of this rewrite.

Related Issue

This pull request should resolve #39.

Context

The previous implementation fully replaced Metro's module system, which could cause compatibility issues with different React Native and Metro versions. The new approach:

  • Surgically patches Metro's __d (define) function: Instead of replacing the entire module system, we wrap the factory function passed to __d to intercept require, importDefault, and importAll parameters
  • Preserves Metro's core functionality: The original __d function is preserved and called with the wrapped factory, ensuring Metro's module initialization logic remains intact
  • Redirects imports through Harness: All require/import calls within modules are redirected through Harness's mocking layer (myRequire, myImportDefault, myImportAll), which then falls back to Metro's original __r system when modules aren't mocked
  • API modifications: The module mocking API has been slightly modified to align with the new implementation. While the core functions (mock, unmock, requireActual, resetModules) remain available, some internal behavior and edge cases may have changed

This surgical approach reduces the risk of breaking changes when React Native or Metro updates their internal module system implementation, as we're only intercepting at the factory level rather than replacing core infrastructure.

Testing

  • All existing module mocking tests pass, including:
    • Basic module mocking functionality
    • Partial mocking (mocking specific exports while preserving others)
    • Unmocking previously mocked modules
    • Module reinitialization after resetModules()
  • Verified that non-mocked modules continue to work normally without interference

@vercel
Copy link

vercel bot commented Jan 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
react-native-harness Ready Ready Preview, Comment Jan 7, 2026 11:23am

@V3RON V3RON merged commit 76dcf48 into main Jan 7, 2026
3 checks passed
@V3RON V3RON deleted the refactor/module-system-patch branch January 7, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Could not patch module system on blank Expo RN project

2 participants